<?php

namespace App\Http\Services;

use App\Http\Model\Hint;
use App\Lib\oss;

class UploadServices extends CommonServices
{   
    private $ossObj = null;

    public function __construct(oss $oss)
    {
        $this->ossObj = $oss;
        parent::__construct();
    }

    public function upload(int $is_md5 = 0, array $upload_config = []) {
        if(empty($upload_config)) return Hint::PARAM_ERROR;
        $size = filesize($_FILES['file']['tmp_name']);
        $fp = fopen($_FILES['file']['tmp_name'], 'rb');
        $content = fread($fp, $size);
        fclose($fp);
        $ossRes = $this->ossObj->getAs($upload_config[0]['put_url'], $content, $size);
        if(!$ossRes) return Hint::FILE_UPLOAD_CONFIG_ERR;
        $data['md5']  = '';
        $data['uri'] = getPicDomain().'/'.$upload_config[0]['uri'];
        if ($is_md5 == 1) $data['md5'] = md5_file($_FILES['file']['tmp_name']);
        return $data;
    }

     /**
     * 获取文件类型
     * @return mixed
     */
    public function get_ext()
    {
        $path_info = pathinfo($_FILES['file']['name']);
        return $path_info['extension'];
    }
}